package com.qiaolang.controller;

import com.qiaolang.Service.UserServiceImpl;
import com.qiaolang.pojo.ID;
import com.qiaolang.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2017/11/12.
 */
@RestController
@CrossOrigin
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserServiceImpl service;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResponseEntity<Users> login(@RequestBody Users users){
        //之前想的太复杂 不需要你在登陆的时候就确定它的权限
        //这样子也没有什么用 只要在后面的某些功能上实现就可以
//        String role=service.getUser(users.getUserno());//得到用户之后以用户的身份登录
//        List<ID> list=service.selectRole(role);//以student这个角色登录
        Users users1=service.login(users.getUserno());
        if(users1!=null){//如果登录成功
            return new ResponseEntity<Users>(users1,HttpStatus.OK);
        }else{
            return new ResponseEntity<Users>(users1,HttpStatus.CONFLICT);
        }
    }


    //这个还需要完善一下子具体什么时候干一些什么事情
    //比如是student就只能查询student自己的个人信息 不能用户管理
    @RequestMapping(value = "/queryall",method = RequestMethod.POST)//加上查表人的身份
    public ResponseEntity<List> queryAll(){
        List<ID> list=service.queryAll();
        if(list.size()>0){
            return new ResponseEntity<List>(list, HttpStatus.OK);
        }else{
            return new ResponseEntity<List>(list, HttpStatus.CONFLICT);
        }
    }

    @RequestMapping(value = "/qiandao/{userno}",method = RequestMethod.POST)
    public ResponseEntity<Void> qiandao(@PathVariable String userno){
        if(service.qiandao(userno)){//如果修改成功
            return new ResponseEntity<Void>(HttpStatus.OK);
        }else{
            return new ResponseEntity<Void>(HttpStatus.CONFLICT);
        }
    }

    @RequestMapping(value = "/chaxun/{userno}",method = RequestMethod.POST)
    public ResponseEntity<String> chaXun(@PathVariable String userno){
        String count=service.chaxun(userno);
        if(count!=null){//如果修改成功
            return new ResponseEntity<String>(count,HttpStatus.OK);
        }else{
            return new ResponseEntity<String>(count,HttpStatus.CONFLICT);
        }
    }

}
